package cn.edu.ujn.liuklab3.controller;

import cn.edu.ujn.liuklab3.entity.*;
import cn.edu.ujn.liuklab3.service.*;
import cn.edu.ujn.util.Page;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@org.springframework.stereotype.Controller
public class Controller {
    @Autowired
    private UserService userService;
    @Autowired
    private RestaurantService restaurantService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;
    @Autowired
    private CouponService couponService;
    @Autowired
    private IngredientstockService ingredientstockService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderdetailService orderdetailService;
    @Autowired
    private TableService tableService;
    @Autowired
    private ReservationService reservationService;
    @Autowired
    private ReviewService reviewService;
    @Autowired
    private UsercouponService usercouponService;
    @ModelAttribute("userMap")
    public Map<Integer, String> getUserMap() {
        List<User> users = userService.findAll();
        Map<Integer, String> userMap = new HashMap<>();
        for (User user : users) {
            userMap.put(user.getUserId(), user.getUserName());
        }
        return userMap;
    }
    @ModelAttribute("restaurantMap")
    public Map<Integer, String> getRestaurantMap() {
        List<Restaurant> restaurants = restaurantService.findAll();
        Map<Integer, String> restaurantMap = new HashMap<>();
        for (Restaurant restaurant : restaurants) {
            restaurantMap.put(restaurant.getRestId(), restaurant.getRestName());
        }
        return restaurantMap;
    }

    @ModelAttribute("couponMap")
    public Map<Integer, String> getCouponMap() {
        List<Coupon> coupons = couponService.findAll();
        Map<Integer, String> couponMap = new HashMap<>();
        for (Coupon coupon : coupons) {
            couponMap.put(coupon.getCouponId(), coupon.getCouponName());
        }
        return couponMap;
    }

    @ModelAttribute("categoryMap")
    public Map<Integer, String> getCategoryMap() {
        List<Category> categories = categoryService.findAll();
        Map<Integer, String> categoryMap = new HashMap<>();
        for (Category cate : categories) {
            categoryMap.put(cate.getCateId(), cate.getCateName());
        }
        return categoryMap;
    }

    @ModelAttribute("dishMap")
    public Map<Integer, String> getDishMap() {
        List<Dish> dishes = dishService.findAll();
        Map<Integer, String> dishMap = new HashMap<>();
        for (Dish dish : dishes) {
            dishMap.put(dish.getDishId(), dish.getDishName());
        }
        return dishMap;
    }

    @GetMapping("/login")
    public String showLoginPage(Model model) {
        return "login";
    }

    @PostMapping("/login")
    public String check(User user, Model model, HttpServletRequest request){
        User user1 = userService.checkLogin(user);
        if(user1 != null){
            HttpSession session = request.getSession();
            session.setAttribute("USER",user1);
            return "user";
        }else{
            model.addAttribute("msg","用户名或密码错误！");
            return "login";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            session.invalidate();
        }
        return "logout";
    }

    @GetMapping("/user")
    public String listUser(@RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "row",defaultValue = "10") Integer rows, Model model, User user) {
        user.setStart((page-1)*rows);
        user.setRows(rows);
        Page<User> page1 = userService.selectAll(user);
        page1.setPage(page);
        model.addAttribute("page1",page1);
        return "user";
    }

    @PostMapping("/user")
    @ResponseBody
    public String createUser(User user) {
        try {
            userService.insert(user);
            return "OK";
        } catch (Exception e) {

            return "FAIL";
        }
    }


    @GetMapping("/restaurant")
    public String listRest(@RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "row",defaultValue = "10") Integer rows, Model model,  Restaurant restaurant) {
        restaurant.setStart((page - 1) * rows);
        restaurant.setRows(rows);
        Page<Restaurant> page2 = restaurantService.selectAll(restaurant);
        page2.setPage(page);
        model.addAttribute("page2", page2);
        return "restaurant";
    }
    // Coupon 列表
    @GetMapping("/coupon")
    public String listCoupon(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "row", defaultValue = "10") Integer rows, Model model, Coupon coupon) {
        coupon.setStart((page - 1) * rows);
        coupon.setRows(rows);
        Page<Coupon> page3 = couponService.selectAll(coupon);
        page3.setPage(page);
        model.addAttribute("page3", page3);
        return "coupon";
    }

    // Dish 列表
    @GetMapping("/dish")
    public String listDish(@RequestParam(value = "page", defaultValue = "1") Integer page,
                           @RequestParam(value = "row", defaultValue = "10") Integer rows, Model model, Dish dish) {
        dish.setStart((page - 1) * rows);
        dish.setRows(rows);
        Page<Dish> page4 = dishService.selectAll(dish);
        page4.setPage(page);
        model.addAttribute("page4", page4);
        return "dish";
    }

    // Ingredientstock 列表
    @GetMapping("/ingredientstock")
    public String listIngredientstock(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "row", defaultValue = "10") Integer rows, Model model, Ingredientstock ingredientstock) {
        ingredientstock.setStart((page - 1) * rows);
        ingredientstock.setRows(rows);
        Page<Ingredientstock> page5 = ingredientstockService.selectAll(ingredientstock);
        page5.setPage(page);
        model.addAttribute("page5", page5);
        return "ingredientstock";
    }

    // Order 列表
    @GetMapping("/order")
    public String listOrder(@RequestParam(value = "page", defaultValue = "1") Integer page,
                            @RequestParam(value = "row", defaultValue = "10") Integer rows, Model model, Order order,Orderdetail orderdetail) {
        order.setStart((page - 1) * rows);
        order.setRows(rows);
        Page<Order> page6 = orderService.selectAll(order);
        page6.setPage(page);
        model.addAttribute("page6", page6);

        orderdetail.setStart((page - 1) * rows);
        orderdetail.setRows(rows);
        Page<Orderdetail> page7 = orderdetailService.selectAll(orderdetail);
        page7.setPage(page);
        model.addAttribute("page7", page7);
        return "order";
    }

    // Table 列表
    @GetMapping("/table")
    public String listTable(@RequestParam(value = "page", defaultValue = "1") Integer page,
                            @RequestParam(value = "row", defaultValue = "10") Integer rows, Model model, Table table) {
        table.setStart((page - 1) * rows);
        table.setRows(rows);
        Page<Table> page8 = tableService.selectAll(table);
        page8.setPage(page);
        model.addAttribute("page8", page8);
        return "table";
    }

    // Reservation 列表
    @GetMapping("/reservation")
    public String listReservation(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                  @RequestParam(value = "row", defaultValue = "10") Integer rows, Model model, Reservation reservation) {
        reservation.setStart((page - 1) * rows);
        reservation.setRows(rows);
        Page<Reservation> page9 = reservationService.selectAll(reservation);
        page9.setPage(page);
        model.addAttribute("page9", page9);
        return "reservation";
    }

    // Review 列表
    @GetMapping("/review")
    public String listReview(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "row", defaultValue = "10") Integer rows, Model model, Review review) {
        review.setStart((page - 1) * rows);
        review.setRows(rows);
        Page<Review> page10 = reviewService.selectAll(review);
        page10.setPage(page);
        model.addAttribute("page10", page10);
        return "review";
    }

    // Usercoupon 列表
    @GetMapping("/usercoupon")
    public String listUsercoupon(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                 @RequestParam(value = "row", defaultValue = "10") Integer rows, Model model, Usercoupon usercoupon) {
        usercoupon.setStart((page - 1) * rows);
        usercoupon.setRows(rows);
        Page<Usercoupon> page11 = usercouponService.selectAll(usercoupon);
        page11.setPage(page);
        model.addAttribute("page11", page11);
        return "usercoupon";
    }

}


